package com.api.shopapi.mapper;

import com.api.shopapi.entity.Mediciner;
import com.api.shopapi.entity.Nurse;
import com.api.shopapi.entity.dto.NurseDTO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface NurseMapper {

    //查询全部护士
    @Select("select n.*,u.Name,u.Phone,u.Status from nurse n,user u" +
            " where" +
            " u.InstitutionCode = #{institutionCode} " +
            " and n.UserCode = u.UserCode ")
    List<NurseDTO> getList(@Param("institutionCode") String institutionCode);

    @Select("<script>" +
            "select n.*,u.Name,u.Phone,u.Status from nurse n,user u where u.InstitutionCode = #{institutionCode} " +
            "and n.UserCode = u.UserCode " +
            "<if test ='name !=null and name!=\"\" '>" +
            "and u.Name like concat('%',#{name},'%') " +
            "</if>" +
            "<if test ='status !=-1 '>" +
            "and u.Status =#{status} " +
            "</if>" +
            " order by u.CreateTime desc"+
            "</script>")
    List<NurseDTO> getPageList(@Param("institutionCode") String institutionCode, @Param("name") String name, @Param("status") int status);

    @Insert("insert into nurse " +
            "(NurseCode," +
            "UserCode," +
            "IdCard," +
            "createTime,"+
            "Address,"+
            "InstitutionCode) " +
            "values (#{nurse.NurseCode} ," +
            "#{nurse.UserCode} ," +
            "#{nurse.IdCard} ," +
            "#{nurse.createTime} ," +
            "#{nurse.Address} ," +
            "#{nurse.InstitutionCode} )")
    int insert(@Param("nurse") Nurse nurse);


    @Insert("<script>" +
            "insert into nurse (NurseCode,UserCode,IdCard,CreateTime,Address,InstitutionCode) values " +

            "<foreach collection='list' item='obj' separator=','>" +
            "(#{obj.NurseCode},#{obj.UserCode},#{obj.IdCard}," +
            "#{obj.CreateTime},#{obj.Address},#{obj.InstitutionCode})" +
            "</foreach>" +

            "</script>")
    Integer batchInsert(List<Nurse> list);

    @Update("update nurse" +
            " set" +
            " userCode=#{nurse.UserCode}," +
            " Address=#{mediciner.Address}," +
            " IdCard=#{nurse.IdCard}" +
            " where NurseCode=#{nurse.NurseCode} ")
    int updateByNurseCode(@Param("nurse") Nurse nurse);

    @Delete("delete from nurse where NurseCode=#{nurseCode} ")
    int delete(@Param("nurseCode") String nurseCode);

    @Select("select  * from  nurse where NurseCode=#{nurseCode} limit 1 ")
    Nurse getByNurseCode(@Param("nurseCode") String nurseCode);

    @Select("select  * from  nurse where UserCode=#{userCode} limit 1 ")
    Nurse getByUserCode(String userCode);


    @Select("<script>" +
            "select * from nurse " +
            " where" +
            " InstitutionCode in (select institutionCode from institution where governmentCode=#{governmentCode} ) " +
            " order by CreateTime desc"+
            "</script>")
    List<NurseDTO> listByGovCode(@Param("governmentCode") String governmentCode);


    @Select("<script>" +
            "select nurseCode from nurse " +
            " where 1=1" +

            "<if test ='institutionCode == \"\" '>" +
            " and InstitutionCode in (select institutionCode from institution where governmentCode=#{governmentCode} )" +
            "</if>" +
            "<if test ='institutionCode != \"\" '>" +
            " and institutionCode = #{institutionCode} " +
            "</if>" +

            " order by CreateTime desc"+
            "</script>")
    List<Nurse> listByGovCodeInsCode(@Param("governmentCode") String governmentCode,
                                        @Param("institutionCode") String institutionCode);


    @Select("<script>" +
            "select nurseCode from nurse " +
            " where" +
            " institutionCode = #{institutionCode} " +
            "</script>")
    List<Nurse> listByInsCode(@Param("institutionCode") String institutionCode);


    @Select("<script>" +
            "select IdCard from nurse where IdCard in" +
            "<foreach collection='list' item='item' open='(' separator=',' close=')' >" +
            "#{item.IdCard}"+
            "</foreach>" +
            "</script>")
    List<String> batchSelectIdCard(List<Nurse> list);

}
